package com.freshfresh.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.freshfresh.activity.R;
import com.freshfresh.utils.FreshConstants;
import com.freshfresh.utils.ImageLoadOptions;
import com.freshfresh.utils.RequestManager;
import com.freshfresh.utils.UrlConstants;
import com.freshfresh.utils.Utils;
import com.hangyjx.widget.dialog.CustomDialog;
import com.nostra13.universalimageloader.core.ImageLoader;
//本地购物车 

public class ShopServerAdapter extends BaseAdapter {
	List<Map<String,Object>> apk_list;
	LayoutInflater inflater;
	Activity context;
	TextView textview;
	TextView textview0;
	TextView textvieww;
	String str_quoteid;
	Dialog progressDialog;
	String userid="";
	String token="";
	public ShopServerAdapter(Activity context, List<Map<String,Object>> apk_list,TextView textvieww,TextView textview,TextView textview0) {
		this.context=context;
		this.textview=textview;
		this.textview0=textview0;
		this.textvieww=textvieww;
		this.apk_list = apk_list;
		this.inflater = LayoutInflater.from(context);
		if (progressDialog == null) {
			progressDialog = Utils.createLoadingDialog(context,
					"正在处理中……");
		}
		
		SharedPreferences share=FreshConstants.getUserShared(context);
		 userid=share.getString("userid", "");
		 token=share.getString("token", "");
		 str_quoteid=share.getString("quoteid", "");
	}
	public void onDateChange(List<Map<String,Object>> apk_list) {
		this.apk_list = apk_list;
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return apk_list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return apk_list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final Map<String,Object> entity = apk_list.get(position);
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.it_shoppingcar, null);
			holder.iv_it_shoppingcar = (ImageView) convertView
					.findViewById(R.id.iv_it_shoppingcar);
			
			holder.tv_it_shoppingcar_del = (ImageView) convertView
					.findViewById(R.id.tv_it_shoppingcar_del);
			
			holder.tv_it_shoppingcar_title = (TextView) convertView
					.findViewById(R.id.tv_it_shoppingcar_title);
			
			
			holder.tv_it_shoppingcar_increase = (TextView) convertView
			.findViewById(R.id.tv_it_shoppingcar_increase);
			holder.tv_it_shoppingcar_decrease = (TextView) convertView
					.findViewById(R.id.tv_it_shoppingcar_decrease);
			
			holder.tv_it_shoppingcar_stock = (TextView) convertView
					.findViewById(R.id.tv_it_shoppingcar_stock);
			holder.tv_it_shoppingcar_price = (TextView) convertView
					.findViewById(R.id.tv_it_shoppingcar_price);
			holder.ed_it_shoppingcar_count = (TextView) convertView
					.findViewById(R.id.ed_it_shoppingcar_count);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		if(entity.get("image")!=null){
			ImageLoader.getInstance().displayImage(entity.get("image").toString(), holder.iv_it_shoppingcar, ImageLoadOptions.getOptions(0));
			}
		holder.tv_it_shoppingcar_title.setText(entity.get("name").toString());
		if(entity.get("is_in_stock").toString().equals("0")){
			holder.tv_it_shoppingcar_stock.setText("缺货");
		}else if(entity.get("is_in_stock").toString().equals("1")){
			holder.tv_it_shoppingcar_stock.setText("现货");
		}
		
		holder.tv_it_shoppingcar_price.setText("¥"+entity.get("special_price").toString());
		holder.ed_it_shoppingcar_count.setText(entity.get("qty").toString());
		final int pos=position;
		holder.tv_it_shoppingcar_del.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String str_itemid=apk_list.get(pos).get("item_id").toString();
				operateDialog(context, str_itemid,"温馨提示", "确认删除这件商品吗？", "确定", "取消");
				
			}
		});
		//加
		
		holder.tv_it_shoppingcar_increase.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				progressDialog.show();
				int mycount=1;
				mycount=Integer.parseInt(holder.ed_it_shoppingcar_count.getText().toString());
				mycount++;
				Map<String,String> map=new HashMap<String, String>();
				map.put("bundle_id", "");
				map.put("item_id", apk_list.get(pos).get("item_id").toString());
				map.put("qty", mycount+"");
				String str_listmap="["+Utils.map2json(map)+"]";
				
				
				
				Map<String, String> params = new HashMap<String, String>();
				params.put("store", "1");
				params.put("quoteid", "");
				params.put("token", token);
				params.put("customerid", userid);
				
				params.put("productsdata", str_listmap);
				
				String ad_url = UrlConstants.UpdateShoppingCar;
				executeRequest(new StringRequest(ad_url, params,
						new Response.Listener<String>() {
							public void onResponse(String arg0) {
								progressDialog.dismiss();
								Log.e("listmap的……", arg0);
								if (Utils.parseJsonStr(arg0).get("result").toString().equals("1")) {
									Map<String,Object> map=(Map<String,Object>)Utils.parseJsonStr(arg0).get("data");
									
									textview.setText("总计： ¥"+map.get("base_grand_total").toString()+"");
									textview.setText("总计： ¥"+map.get("base_grand_total").toString()+"");
									textvieww.setText("共"+map.get("product_total").toString()+"件商品");
									List<Map<String,Object>> listmap_serve=(List<Map<String,Object>>)map.get("product_info");
									onDateChange(listmap_serve);
								} else {
									
								}
							}

						}, new Response.ErrorListener() {

							public void onErrorResponse(VolleyError arg0) {
								progressDialog.dismiss();
								Toast.makeText(context, "添加失败", 1).show();
							}
						}));
				
			}
		});
		//减
		holder.tv_it_shoppingcar_decrease.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				int mycount=1;
				mycount=Integer.parseInt(holder.ed_it_shoppingcar_count.getText().toString());
				mycount--;
				if(mycount<1){
					Toast.makeText(context, "该商品至少购买一件", 0).show();
				}else{
				progressDialog.show();
				Map<String,String> map=new HashMap<String, String>();
				map.put("bundle_id", "");
				map.put("item_id", apk_list.get(pos).get("item_id").toString());
				map.put("qty", mycount+"");
				String str_listmap="["+Utils.map2json(map)+"]";
				
				
				Map<String, String> params = new HashMap<String, String>();
				params.put("store", "1");
				params.put("quoteid", "");
				params.put("token", token);
				params.put("customerid", userid);
				
				params.put("productsdata", str_listmap);
				
				String ad_url = UrlConstants.UpdateShoppingCar;
				executeRequest(new StringRequest(ad_url, params,
						new Response.Listener<String>() {
							public void onResponse(String arg0) {
								progressDialog.dismiss();
								Log.e("listmap的……", arg0);
								if (Utils.parseJsonStr(arg0).get("result").toString().equals("1")) {
									Map<String,Object> map=(Map<String,Object>)Utils.parseJsonStr(arg0).get("data");
									
									textview.setText("总计： ¥"+map.get("base_grand_total").toString()+"");
									textview.setText("总计： ¥"+map.get("base_grand_total").toString()+"");
									textvieww.setText("共"+map.get("product_total").toString()+"件商品");
									
									List<Map<String,Object>> listmap_serve=(List<Map<String,Object>>)map.get("product_info");
									onDateChange(listmap_serve);
								} else {
									
								}
							}

						}, new Response.ErrorListener() {

							public void onErrorResponse(VolleyError arg0) {
								progressDialog.dismiss();
								Toast.makeText(context, "操作失败", 1).show();
							}
						}));
				}
			}
		});
		return convertView;
	}
	class ViewHolder {
		ImageView iv_it_shoppingcar;//图片
		ImageView tv_it_shoppingcar_del;//删除
		TextView tv_it_shoppingcar_title;//名称 
		TextView tv_it_shoppingcar_stock;//库存
		TextView tv_it_shoppingcar_price;//价格
		TextView ed_it_shoppingcar_count;//数量
		
		TextView tv_it_shoppingcar_increase;//加
		TextView tv_it_shoppingcar_decrease;//减
	}
	
	protected void executeRequest(Request request) {
		RequestManager.addRequest(request, this);
	}
			 
	//dialog
	public  void operateDialog(final Context context,final String itemid,String title,String message,
			String positiveButtonText,String negativeButtonText){
		CustomDialog.Builder builder = new CustomDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(message);
		//确定按钮
		builder.setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				
				progressDialog.show();
				
				
				Map<String, String> params = new HashMap<String, String>();
				params.put("store", "1");
				params.put("quoteid", str_quoteid);
				params.put("token", token);
				params.put("customerid", userid);
				params.put("itemid", itemid);
				
				String ad_url = UrlConstants.DeleteShoppingCar;
				executeRequest(new StringRequest(ad_url, params,
						new Response.Listener<String>() {
							public void onResponse(String arg0) {
								progressDialog.dismiss();
								Log.e("listmap的……", arg0);
								if (Utils.parseJsonStr(arg0).get("result").toString().equals("1")) {
									Map<String,Object> map=(Map<String,Object>)Utils.parseJsonStr(arg0).get("data");
									
									
									textview.setText("总计： ¥"+map.get("base_grand_total").toString()+"");
									textview.setText("总计： ¥"+map.get("base_grand_total").toString()+"");
									textvieww.setText("共"+map.get("product_total").toString()+"件商品");
									
									List<Map<String,Object>> listmap_serve=(List<Map<String,Object>>)map.get("product_info");
									onDateChange(listmap_serve);
									Toast.makeText(context, "删除成功", 1).show();
								} else {
									
								}
							}

						}, new Response.ErrorListener() {

							public void onErrorResponse(VolleyError arg0) {
								progressDialog.dismiss();
								Toast.makeText(context, "操作失败", 1).show();
							}
						}));
				
			}
		});
		//取消按钮
		builder.setNegativeButton(negativeButtonText, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.createB2().show();
	}		 
}
